package com.goldcn.goods.service;

import com.goldcn.common.CommonTools;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.common.util.CommonUtils;
import com.goldcn.common.util.IdGen;
import com.goldcn.enumeration.ErrorCodeEnum;
import com.goldcn.goods.dao.GoodsLabelMapper;
import com.goldcn.goods.dto.GoodsLabelDto;
import com.goldcn.goods.model.GoodsLabel;
import com.goldcn.goods.vo.GoodsLabelVo;
import com.goldcn.interceptor.SystemContext;
import com.google.common.base.Objects;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2016/11/15.
 */
@Service
public class GoodsLabelService {
    @Autowired
    private GoodsLabelMapper goodsLabelMapper;

    /**
     * 得到label集合
     * @return
     */
    @Transactional
    public List<GoodsLabelDto> getGoodsLabelList(Long companyID){
        return goodsLabelMapper.getGoodsLabelList(companyID);
    }

    /**
     * 把GoodsLabel对象插入进去
     * @param goodsLabelVo
     * @return
     */
    @Transactional
    public Integer addGoodsLabel(GoodsLabelVo goodsLabelVo){
        if(CommonUtils.isEmpty(goodsLabelVo.getName())){
            return 0;
        }
        GoodsLabel goodsLabel = new GoodsLabel();
        BeanUtils.copyProperties(goodsLabelVo,goodsLabel);
        if(goodsLabelVo.getCompanyID() ==null || Objects.equal(goodsLabelVo.getCompanyID(),0L)){
            throw new ServiceException("公司ID不能为空");
        }
        this.verifyLabelName(goodsLabelVo.getName(),goodsLabelVo.getCompanyID());
        goodsLabel.setLabelID(IdGen.get().nextId()); //进来的数据没有主见 这里需要设置一下
        goodsLabel.setAddTime(CommonUtils.getCurTimestamp());
        goodsLabel.setUpdateTime(CommonUtils.getCurTimestamp());
        goodsLabel.setAddUser(goodsLabelVo.getOperateUserId());
        goodsLabel.setCompanyID(goodsLabelVo.getCompanyID());//赋值公司ID
        goodsLabel.setSeqno(goodsLabelVo.getSeqno());
        Integer result =  goodsLabelMapper.insertSelective(goodsLabel);
        if(result != 1){
            throw CommonTools.createException(ErrorCodeEnum.addLabelFailure);
        }
        return result;
    }

    /**
     * 通过labelID删除指定的label
     * @param labelID
     * @return
     */
    @Transactional
    public Integer deleteByPrimaryKey(Long labelID){
        //判断标签是否使用
        if(goodsLabelMapper.labelUseCount(labelID)>0){
            throw CommonTools.createException(ErrorCodeEnum.lableIsUsed);
        }
        Integer result = goodsLabelMapper.deleteByPrimaryKey(labelID);
        if(result!=1){
            //抛出异常
            throw CommonTools.createException(ErrorCodeEnum.deleteLabelFailure);
        }
        return result;
    }

    /**
     * 更新label
     * @param goodsLabelVo
     * @return
     */

    public Integer updateByGoodsLabel(GoodsLabelVo goodsLabelVo){

       /*if( CommonUtils.isEmpty(goodsLabelVo.getBanner())){
            return 0;
        }*/
        GoodsLabel goodLabel = new GoodsLabel();
        BeanUtils.copyProperties(goodsLabelVo,goodLabel);
        goodLabel.setUpdateTime(CommonUtils.getCurTimestamp());
        goodLabel.setUpdateUser(goodsLabelVo.getOperateUserId());
        Integer result = goodsLabelMapper.updateByPrimaryKeySelective(goodLabel);
        if(result != 1){
            throw CommonTools.createException(ErrorCodeEnum.updateLabelFailure);
        }
        return result;
    }

    /**
     *根据labelId得到goodsLabel对象
     * @param labelID
     * @return
     */
    public GoodsLabel selectByPrimaryKey(Long labelID){
        return goodsLabelMapper.selectByPrimaryKey(labelID);
    }

    private void verifyLabelName(String name,Long companyID){
        Integer result = goodsLabelMapper.IsExistsGoodsLabel(name,companyID).size();
        if(result !=0){
            throw CommonTools.createException(ErrorCodeEnum.addLabelReanem);
        }
    }

    public Object deleteByCompanyID(Long companyID){
        return  goodsLabelMapper.deleteByCompanyID(companyID);
    }
}
